<!--
  @Description:角色基础信息配置
  @Author:zqy
  @Date:2022-12-15 14:17:26
  @Last Modified by:zqy
  @Last Modified time:2022-12-15 14:17:26
-->
<template>
  <el-dialog
    ref="formDlg"
    title="角色基础信息"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="500px"
    @close="closeDialog"
  >
    <div style="padding-right:30px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="角色名称:" prop="name">
              <el-input v-model.trim="formData.name" maxlength="25" show-word-limit placeholder="请输入页面名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型:" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio :label="1">生效</el-radio>
                <el-radio :label="0">不生效</el-radio>
                <el-radio :label="2">定时生效</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col
            v-if="['2',2].includes(formData.status)"
            :span="24"
          >
            <el-form-item label="类型:" prop="effectiveBeginTime" :rules="[{required:['2',2].includes(formData.status),message:'请选择开始生效时间',trigger:'change'}]">
              <el-date-picker
                v-model="formData.effectiveBeginTime"
                type="datetime"
                placeholder="选择开始生效时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item></el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
      <el-button size="mini" type="primary" @click="submitForm('config')">保存并去配置</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveRole } from '@/api/roleConfig'
export default {
  name: 'RoleConfigEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id: null,
        name: '',
        status: '',
        effectiveBeginTime: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      submiting: false
    }
  },

  computed: {
    dlgTitle() {
      return this.formData && this.formData.id ? '编辑角色基础信息配置' : '新增角色基础信息配置'
    },
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.detail && this.detail.id) {
      const data = this.detail
      const status = [2, '2', 1, '1'].includes(data.status) ? data.status : 0
      const effectiveBeginTime = [2, '2'].includes(data.status) ? data.effectiveBeginTime : ''
      this.formData = {
        id: data.id,
        name: data.name,
        effectiveBeginTime,
        status
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        id: null,
        name: '',
        effectiveBeginTime: '',
        status: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const formData = Object.assign(this.formData)
          this.submiting = true
          await saveRole(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: `${formData.id ? '编辑' : '新增'}保存成功`,
                type: 'success'
              })
              this.closeDialog()
              let data = {
                id: (this.detail && this.detail.id) || res.msg,
                name: formData.name,
                status: formData.status,
                effectiveBeginTime: formData.effectiveBeginTime
              }
              if (formData.id) {
                data = {
                  ...this.detail,
                  ...formData
                }
              }
              const result = {
                data,
                goConfig: type === 'config'
              }
              this.$emit('success', result)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
